
clear all
set maxvar 17000
graph set window fontface "Times New Roman"
set more off, perm

cap noisily cd "/Users/aspearot/Dropbox/MaLT 2017/Draft/submission_draft/ReStat/resubmission 2/replication package/malt/"

global model "Model"


use "Data/MaLT ProdFunEstimation_NPS_2010_2012.dta", clear


*keep if region_str=="KILIMANJARO"|region_str=="ARUSHA"|region_str=="MANYARA"|region_str=="TANGA"

******  First, calculate the fert expenditure share using imputed wages ******

count 
su wage_planting_w1
su wage_weeding_w1 
su wage_fert_w1 
su wage_harv_w1

egen wage_planting_v1_district = median(wage_planting_w1), by(district)
egen wage_weeding_w1_district = median(wage_weeding_w1), by(district)
egen wage_fertilizing_w1_district = median(wage_fert_w1), by(district)
egen wage_harvesting_w1_district = median(wage_harv_w1), by(district)

su wage_planting_v1_district
su wage_weeding_w1_district 
su wage_fertilizing_w1_district 
su wage_harvesting_w1_district

gen avg_wage=(wage_planting_v1_district*all_labordays_planting_w1+wage_weeding_w1_district*all_labordays_weeding_w1+wage_fertilizing_w1_district*all_labordays_fert_w1+wage_harvesting_w1_district*all_labordays_harv_w1)/(all_labordays_planting_w1+all_labordays_weeding_w1+all_labordays_fert_w1+all_labordays_harv_w1) 

gen wage_exp=(wage_planting_v1_district*all_labordays_planting_w1+wage_weeding_w1_district*all_labordays_weeding_w1+wage_fertilizing_w1_district*all_labordays_fert_w1+wage_harvesting_w1_district*all_labordays_harv_w1)

su avg_wage
su wage_exp

gen fertexpshare = fert_exp_total_w1/(fert_exp_total_w1+wage_exp)


gen beta = 1-fertexpshare

gen fert_rev = fert_exp_total_w1/harvTSH



gen input_rev = (fert_exp_total_w1+wage_exp)/harvTSH

gen wage_rev = (wage_exp)/harvTSH

tabstat fertexpshare fert_rev input_rev wage_rev, stat(mean median)

tabstat fertexpshare fert_rev input_rev wage_rev if region_str=="KILIMANJARO"|region_str=="MANYARA", stat(mean median)

tabstat fertexpshare fert_rev, by(region_str) stat(mean median)




winsor harvKG, generate(harvKG_w5) p(.05)

gen ln_harv = log(harvKG_w5)
gen ln_harv_w1 = log(harvKG_w1)
gen ln_labor = log(all_labordays)
gen ln_land = log(ag2a_09)

gen ln_harv_land = log(harvKG/ag2a_09)
gen ln_labor_land = log(all_labordays/ag2a_09)



egen id=group(hhid)
egen id_plot=group(hhid plotnum)
egen id_plot_usefert=group(hhid plotnum usefert)

egen id_panel=group(hhid panel)
egen id_panel_usefert=group(hhid panel usefert)

egen district_panel = group(district panel)
egen district_usefert = group(district panel usefert)
egen ward_panel = group(ward panel)
egen ward_usefert = group(ward panel usefert)
egen ea_panel = group(ea panel)
egen ea_usefert = group(ea panel usefert)


gen ln_land_fert = ln_land*usefert
gen ln_labor_fert = ln_labor*usefert
gen ln_labor_land_fert = ln_labor_land*usefert

gen ln_land_nofert = ln_land*(1-usefert)
gen ln_labor_nofert = ln_labor*(1-usefert)

*keep if region_str=="ARUSHA"|region_str=="KILIMANJARO"|region_str=="MANYARA"

egen avgfert= mean(usefert), by(id)
gen ln_fert = ln(fertKG)


xtlogit usefert ln_land, i(ea_panel) fe
xtlogit usefert ln_land, i(id_panel) fe


reghdfe ln_harv ln_land ln_labor ln_fert if usefert==1, absorb(id district_panel)
outreg2 ln_land ln_labor ln_fert `farmer_controls' using "Results/Production_Function_Parameters.xls", excel nocons replace addtext(ID, Y, Plot, N, Dist-Year, Y, EA-Year, N, ID-Year, N) addstat(beta,_b[ln_fert]+_b[ln_labor], gamma,_b[ln_labor]/(_b[ln_fert]+_b[ln_labor]),epsilon, 10*(1-_b[ln_fert]-_b[ln_labor])/_b[ln_fert])

reghdfe ln_harv ln_land ln_labor ln_fert if usefert==1, absorb(id_plot district_panel)
outreg2 ln_land ln_labor ln_fert `farmer_controls' using "Results/Production_Function_Parameters.xls", excel nocons addtext(ID, N, Plot, Y, Dist-Year, Y, EA-Year, N, ID-Year, N) addstat(beta,_b[ln_fert]+_b[ln_labor], gamma,_b[ln_labor]/(_b[ln_fert]+_b[ln_labor]),epsilon, 10*(1-_b[ln_fert]-_b[ln_labor])/_b[ln_fert])

reghdfe ln_harv ln_land ln_labor ln_fert if usefert==1, absorb(id_plot ea_panel)
outreg2 ln_land ln_labor ln_fert `farmer_controls' using "Results/Production_Function_Parameters.xls", excel nocons addtext(ID, N, Plot, Y, Dist-Year, N, EA-Year, Y, ID-Year, N) addstat(beta,_b[ln_fert]+_b[ln_labor], gamma,_b[ln_labor]/(_b[ln_fert]+_b[ln_labor]),epsilon, 10*(1-_b[ln_fert]-_b[ln_labor])/_b[ln_fert])

reghdfe ln_harv ln_land ln_labor ln_fert if usefert==1, absorb(id_panel)
outreg2 ln_land ln_labor ln_fert `farmer_controls' using "Results/Production_Function_Parameters.xls", excel nocons addtext(ID, N, Plot, N, Dist-Year, N, EA-Year, N, ID-Year, Y) addstat(beta,_b[ln_fert]+_b[ln_labor], gamma,_b[ln_labor]/(_b[ln_fert]+_b[ln_labor]),epsilon, 10*(1-_b[ln_fert]-_b[ln_labor])/_b[ln_fert])

reghdfe ln_harv ln_land ln_labor ln_fert if usefert==1, absorb(id_plot id_panel)
outreg2 ln_land ln_labor ln_fert `farmer_controls' using "Results/Production_Function_Parameters.xls", excel nocons addtext(ID, N, Plot, Y, Dist-Year, N, EA-Year, N, ID-Year, Y) addstat(beta,_b[ln_fert]+_b[ln_labor], gamma,_b[ln_labor]/(_b[ln_fert]+_b[ln_labor]),epsilon, 10*(1-_b[ln_fert]-_b[ln_labor])/_b[ln_fert])







